package se.cbb.lib;

import java.io.File;
import java.util.ArrayList;

public abstract class UppmaxCoreClusterScripting {
	private static int index = 0;
	public static void generateScript(String path, String project, String time, String jobName, String fileName, String simRunIdentifier, String maxMemory, String minMemory) {
		String path1 = path + "GeneTrees/";
		File folder = new File(path1);
		File[] listOfFiles = folder.listFiles();
		int numberofFiles = listOfFiles.length;
		ArrayList<String> list = new ArrayList<String>();
		index = numberofFiles;
		
		for(int i = 0; i < numberofFiles; i++) {
			list.add(listOfFiles[i].getName().substring(0, listOfFiles[i].getName().length()-4));
		}
		try {
			for(int i = 1; i < (numberofFiles) + 1; i++) {
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#!/bin/bash");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -A " + project);
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -p core");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -n 1");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -t " + time);
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -J " + jobName + simRunIdentifier + "-" + i);
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "java -Xmx" + maxMemory + " -Xms" + minMemory + " -jar jprime-0.0.1-SNAPSHOT.jar Delirious RealTree.nwk ./Sequences/" + list.get(i-1) + ".fa ./Mapping/" + list.get(i-1) + ".map -o " + list.get(i-1) + ".mcmc -sm JTT -i 5000000");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void generateInformedScript(String path, String project, String time, String jobName, String fileName, String simRunIdentifier, String maxMemory, String minMemory, String duprate, String lossrate) {
		String path1 = path + "GeneTrees/";
		File folder = new File(path1);
		File[] listOfFiles = folder.listFiles();
		int numberofFiles = listOfFiles.length;
		ArrayList<String> list = new ArrayList<String>();
		index = numberofFiles;
		
		for(int i = 0; i < numberofFiles; i++) {
			list.add(listOfFiles[i].getName().substring(0, listOfFiles[i].getName().length()-4));
		}
		try {
			for(int i = 1; i < (numberofFiles) + 1; i++) {

				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#!/bin/bash");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -A " + project);
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -p core");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -n 1");
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -t " + time);
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "#SBATCH -J " + jobName + simRunIdentifier + "-I" + i);
				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "");

				SequenceFileWriter.writeAndAppendLine(path, fileName + i + ".sh", "java -Xmx" + maxMemory + " -Xms" + minMemory + " -jar jprime-0.0.1-SNAPSHOT.jar Delirious RealTree.nwk ./Sequences/" + list.get(i-1) + ".fa ./Mapping/" + list.get(i-1) + ".map -o Informed" + list.get(i-1) + ".mcmc -sm JTT -erpdm 1.0 -dup " + duprate +  " -loss " + lossrate + " -g ./PrimeTrees/" + list.get(i-1) + ".nwk -i 5000000");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void generateRunInformedScript(String path) {
		try {
			SequenceFileWriter.writeAndAppendLine(path, "InformedRunScript.sh", "#!/bin/bash");
			
			for(int i = 1; i < (index) + 1; i++) 
				SequenceFileWriter.writeAndAppendLine(path, "InformedRunScript.sh", "sbatch jprimeScriptInformed" + i + ".sh");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void generateRunScript(String path) {
		try {
			SequenceFileWriter.writeAndAppendLine(path, "RunScript.sh", "#!/bin/bash");
			
			for(int i = 1; i < (index) + 1; i++) 
				SequenceFileWriter.writeAndAppendLine(path, "RunScript.sh", "sbatch jprimeScript" + i + ".sh");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
